import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({
  // 配置进度条
  showSpinner: false
})

// console.log(import.meta.env.BASE_URL) // 来源于 vite.config.ts 中base属性

// new VueRouter() => createRouter() 得到一个路由实例
const router = createRouter({
  // mode: hash/history
  // 配置路由模式
  // createWebHistory()      得到一个history模式
  // createWebHashHistory()  得到一个hash模式

  // import.meta.env.BASE_URL
  history: createWebHistory(import.meta.env.BASE_URL), // 参数设置项目预览的基地址  www.xx.com/hhh
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login/index.vue'),
      meta: {
        title: '登录',
        xx: 'hh'
      } // router规则中的额外数据对象
    },
    {
      path: '/',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页', xx: 'hh' }
        },
        {
          path: '/article',
          component: () => import('@/views/Article/index.vue'),
          meta: { title: '百科' }
        },
        {
          path: '/notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: { title: '通知' }
        },
        {
          path: '/user',
          component: () => import('@/views/User/index.vue'),
          meta: { title: '我的' }
        }
      ]
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User/PatientPage.vue'),
      meta: { title: '家庭档案' }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User/ConsultPage.vue'),
      meta: { title: '问诊记录' }
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },

      // 路由页面进入前
      beforeEnter(to) {
        // 如果未支付成功 那么回到待支付页面 继续支付
        // 细节: 地址栏中的参数都是字符串
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/User/ConsultDetail.vue'),
      meta: { title: '问诊详情' }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/Order/OrderPay.vue'),
      meta: { title: '药品支付' }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/Order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/Order/OrderDetail.vue'),
      meta: { title: '药品订单详情' }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/Order/OrderLogistics.vue'),
      meta: { title: '物流信息详情' }
    }
  ]
})

const WHITE_LIST = ['/login', '/register']

router.beforeEach((to) => {
  NProgress.start() // 打开进度条

  // 这个项目中
  // 如果没登录 且要去白名单之外的页面 拦截去登录
  const store = useUserStore()

  if (!store.user?.token && !WHITE_LIST.includes(to.path)) return '/login'
  // 如果不满足以上条件 那么直接结束 连return都没 直接放行
})

// 后置守卫将来有一种情况是不触发的 被拦截的 => vue2的bug(v3已经解决了)
router.afterEach((to) => {
  NProgress.done() // 关闭进度条
  // 页面一进入 修改标题
  // console.log(to, 'to-----')
  document.title = '欢迎进入' + to.meta.title
})

export default router

/* 
vue2
  router.beforeEach((to, from, next) => {
    to 去哪
    from 来自哪
    next 放行函数 => next()   next('/login')
  })

vue3
  router.beforeEach((to, from) => {
    to 去哪
    from 来自哪
    没有next  怎么放行?
    v3中不需要next函数放行=>如果需要放行 那么不return就可以; 如果需要拦截 return '路径'
  })
*/
